package com.gaussian.property.module.biz.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import com.gaussian.property.module.biz.entity.ProjectReportFile;
import com.gaussian.property.module.biz.entity.ReportFile;
import com.gaussian.property.module.biz.mapper.ProjectReportFileMapper;
import com.gaussian.property.module.biz.service.IProjectReportFileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaussian.property.module.biz.service.IReportFileService;
import com.gaussian.property.module.biz.vo.projectreport.AddProjectReport;
import com.gaussian.property.module.biz.vo.projectreport.ProjectReportFileVO;
import com.songqi.common.core.exception.CustomException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 项目报告 服务实现类
 * </p>
 *
 * @author songqi(13776346982)
 * @since 2024-08-11
 */
@Service
public class ProjectReportFileServiceImpl extends ServiceImpl<ProjectReportFileMapper, ProjectReportFile> implements IProjectReportFileService {

    @Resource
    private IReportFileService iReportFileService;

    @Override
    public boolean checkReportIsExist(String projectId, String month,  String reportType, String fileName){
        ProjectReportFileVO projectReportFileVo = baseMapper.getProjectReportFile(projectId, month, reportType, fileName);
        return projectReportFileVo != null;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addProjectReport(AddProjectReport addProjectReport) {
        String fileName = addProjectReport.getFile().getFileName();
        String extName = FileNameUtil.extName(fileName);
        String reportType = addProjectReport.getReportType();
        String month = addProjectReport.getMonth();
        //检查报告是否已上传
        for(String projectId : addProjectReport.getProjectIdList()){
            ProjectReportFileVO projectReportFileVo = baseMapper.getProjectReportFile(projectId, addProjectReport.getMonth(), reportType, fileName);
            if(projectReportFileVo != null){
                throw new CustomException(String.format("%s项目%s月份的报告已存在", projectReportFileVo.getProjectName(), projectReportFileVo.getMonth()));
            }
        }
        ReportFile reportFile = new ReportFile();
        reportFile.setFileName(fileName);
        reportFile.setFileExt(extName);
        reportFile.setFilePath(addProjectReport.getFile().getPath());
        reportFile.setCreateBy("admin");
        reportFile.setUpdateBy("admin");
        iReportFileService.save(reportFile);
        String reportFileId = reportFile.getReportFileId();
        for(String projectId : addProjectReport.getProjectIdList()){
            ProjectReportFile projectReportFile = new ProjectReportFile();
            projectReportFile.setReportFileId(reportFileId);
            projectReportFile.setProjectId(projectId);
            projectReportFile.setReportType(reportType);
            projectReportFile.setMonth(month);
            projectReportFile.setCreateBy("admin");
            projectReportFile.setUpdateBy("admin");
            super.save(projectReportFile);
        }
    }

}
